package com.wuliu.controller;

import com.wuliu.common.constant.OrderConstants;
import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.core.domain.PageResult;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.TspOrderPre;
import com.wuliu.system.domain.dto.app.BatchCollectOrderDto;
import com.wuliu.system.domain.dto.app.CalcOrderFeeDto;
import com.wuliu.system.domain.dto.app.RecOrderPreDto;
import com.wuliu.system.domain.dto.app.UserOrderPreDto;
import com.wuliu.system.domain.dto.applet.QryOrderFeeDto;
import com.wuliu.system.domain.vo.applet.TspOrderPreVo;
import com.wuliu.system.service.app.AppTspOrderPreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author realue
 * @version 1.0
 * @date 2023-11-03 17:38
 */
@RestController
@RequestMapping("/app/order")
@Api(tags = {"预约单控制器"})
public class TspOrderPreController {

    @Resource
    private AppTspOrderPreService appTspOrderPreService;


    @GetMapping("/getOrderPres")
    @ApiOperation(value = "我的预约单", notes = "")
    public ApiResult<PageResult<TspOrderPreVo>> getOrderPres(@Validated UserOrderPreDto dto) {
        if (dto.getStatus() == OrderConstants.ORDER_PRE_STATUS_0 && StringUtils.isAnyBlank(dto.getLat(), dto.getLng())) {
            throw new ServiceException("业务员经纬度不能为空");
        }
        return ApiResult.success(appTspOrderPreService.getOrderPres(dto));
    }

    @GetMapping("/getInfo/{id}")
    @ApiOperation(value = "预约单详细", notes = "")
    public ApiResult<TspOrderPre> getInfo(@PathVariable("id") String id) {
        return ApiResult.success(appTspOrderPreService.getInfo(id));
    }

    @PostMapping("/receiveOrder")
    @ApiOperation(value = "接单操作", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "运单id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "操作类型：1.接单 2.取消接单", dataType = "integer", paramType = "query")
    })
    public ApiResult<Boolean> receiveOrder(String orderId, Integer type) {
        if (StringUtils.isEmpty(orderId)) {
            return ApiResult.error("运单号不能为空");
        }
        if (type != 1 && type != 2) {
            return ApiResult.error("操作类型不正确");
        }
        return ApiResult.success(appTspOrderPreService.receiveOrder(orderId, type));
    }

    @PostMapping("/batchCollect")
    @ApiOperation(value = "批量揽收", notes = "")
    public ApiResult<Boolean> batchCollect(@RequestBody @Validated BatchCollectOrderDto dto) {
        return ApiResult.success(appTspOrderPreService.batchCollect(dto));
    }

    @PostMapping("/collect")
    @ApiOperation(value = "单票揽收", notes = "")
    public ApiResult<Boolean> collect(@RequestBody @Validated RecOrderPreDto dto) {
        return ApiResult.success(appTspOrderPreService.collect(dto));
    }

    @GetMapping("/calcFee")
    @ApiOperation(value = "计算运费", notes = "")
    public ApiResult<BigDecimal> calcFee(@Validated CalcOrderFeeDto dto) {
        return ApiResult.success(appTspOrderPreService.calcFee(dto));
    }
}
